<?php

namespace app\api\controller;


use app\api\controller\BaseController;
use app\common\exception\MessageException;
use app\common\exception\MissException;
use app\common\validate\IDMustBePositiveInt;
use app\common\model\mysql\Article as ArticleModel;
use app\common\model\mysql\ArticleCategory as ArticleCategoryModel;

class Article extends BaseController
{

    /**
     * @notes 文章列表
     * @return \think\response\Json
     * @author 段誉
     * @date 2022/9/20 15:30
     */
    public function list()
    {
        try {
            $params = $this->request->param();
            $params['status'] = 1;
            $result = ArticleModel::list($params);

            return $this->renderSuccess($result);
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    /**
     * @param $id
     * @return ArticleModel|array|mixed|\think\Model
     * @throws MissException
     * @throws \app\common\exception\ParameterException
     */
    public function getDetail($id)
    {
        $validate = new IDMustBePositiveInt();
        $validate->goCheck();
        $category = ArticleModel::findOrEmpty($id);
        if(empty($category)){
            throw new MissException([
                'msg' => 'category not found'
            ]);
        }
        return $category;
    }

    /**
     * 获取全部类目列表，但不包含类目下的商品
     * Request 演示依赖注入Request对象
     * @url /category/all
     * @return array of Categories
     * @throws MissException
     */
    public function getAllCate()
    {
        try {
            $params = $this->request->param();
            $params['status'] = 1;
            $result = ArticleCategoryModel::list($params);
            return $this->renderSuccess($result);
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }
}